package 测试;

public class 最长回文子串 {

	public static void main(String[] args) {

	}

	public class Palindrome {
		public int getLongestPalindrome(String A, int n) {

			if (A == null || n == 0)
				return 0;

			int maxLen = 0;
			char[] ch = A.toCharArray();

			for (int i = 0; i < n; ++i) {
				// 按奇回文来判断
				int j = 1; // 从位置i往两边扩的长度
				int curLen = 1; // 当前回文子串长度

				while (i - j >= 0 && i + j < n && ch[i - j] == ch[i + j]) {
					curLen += 2;
					++j;
				}
				maxLen = Math.max(curLen, maxLen);

				// 按偶回文来判断
				j = 0;
				curLen = 0;

				while (i - j >= 0 && i + j + 1 < n && ch[i - j] == ch[i + j + 1]) {
					curLen += 2;
					++j;
				}
				maxLen = Math.max(curLen, maxLen);
			}

			return maxLen;

		}
	}

}
